home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_config.lha / Config / config.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-28  |  8.2 KB  |  265 lines

  1. /*
  2. ** Module:    Config.
  3. ** Authors:   Paul Manias & Peter Cahill.
  4. ** Copyright: DreamWorld Productions (c) 1998.  All rights reserved.
  5. **
  6. ** --------------------------------------------------------------------------
  7. ** 
  8. ** TERMS AND CONDITIONS
  9. ** 
  10. ** This source code is made available on the condition that it is only used to
  11. ** further enhance the Games Master System.  IT IS NOT DISTRIBUTED FOR THE USE
  12. ** IN OTHER PRODUCTS.  Developers may edit and re-release this source code
  13. ** only in the form of its GMS module.  Use of this code outside of the module
  14. ** is not permitted under any circumstances.
  15. ** 
  16. ** This source code stays the copyright of DreamWorld Productions regardless
  17. ** of what changes or additions are made to it by 3rd parties.  A joint
  18. ** copyright can be granted if the 3rd party wishes to retain some ownership
  19. ** of said modifications.
  20. ** 
  21. ** In exchange for our distribution of this source code, we also ask you to
  22. ** distribute the source when releasing a modified version of this module.
  23. ** This is not compulsory if any additions are sensitive to 3rd party
  24. ** copyrights, or if it would damage any commercial product(s).
  25. ** 
  26. ** --------------------------------------------------------------------------
  27. **
  28. ** BUGS AND MISSING FEATURES
  29. ** -------------------------
  30. ** If you correct a bug or fill in a missing feature, the source should be
  31. ** e-mailed to pmanias@ihug.co.nz for inclusion in the next update of this
  32. ** module.
  33. **
  34. **   + Implement SaveToFile() through use of the Config->Entry array.
  35. **
  36. ** CHANGES
  37. ** -------
  38. ** -1998-
  39. ** 28 Jul Module started.
  40. ** 07 Aug All module functions successfully tested.
  41. **
  42. **        VERSION 2.1
  43. ** 30 Aug Fixed bug in leading/trailing spaces around the '=' sign of items.
  44. **        Fixed buffer allocation bug.
  45. ** 11 Sep Items can now start with a number as the first character.
  46. */
  47.  
  48. #include <proto/dpkernel.h>
  49. #include <desktop/all.h>
  50. #include <system/all.h>
  51. #include <misc/config.h>
  52. #include <pragmas/strings_pragmas.h>
  53. #include <pragmas/config_pragmas.h>
  54. #include "defs.h"
  55.  
  56. /***********************************************************************************/
  57.  
  58. #define CONFIG_FIELDS 2
  59.  
  60. struct Field ConfigFields[CONFIG_FIELDS] = {
  61.   { "Source", 12, FID_Source, FDF_SOURCE,    0, 0, NULL, NULL },
  62.   { "Data",   16, FID_Data,   FDF_BYTEARRAY, 0, 0, NULL, NULL },
  63. };
  64.  
  65. struct Function JumpTableV1[] = {
  66.   { LIBReadConfig,    "ReadConfig(a0l,a1l,a2l)"    },
  67.   { LIBReadConfigInt, "ReadConfigInt(a0l,a1l,a2l)" },
  68.   { NULL, NULL }
  69. };
  70.  
  71. BYTE ModAuthor[]    = "Paul Manias";
  72. BYTE ModDate[]      = "September 1998";
  73. BYTE ModCopyright[] = "DreamWorld Productions (c) 1998.  All rights reserved.";
  74. BYTE ModName[]      = "Config";
  75.  
  76. /************************************************************************************
  77. ** Command:  Init()
  78. **
  79. ** Called when our module is being opened for the first time.
  80. */
  81.  
  82. LIBFUNC LONG CMDInit(mreg(__a0) struct Module  *argModule,
  83.                      mreg(__a1) struct DPKBase *argDPKBase,
  84.                      mreg(__a2) struct GVBase  *argGVBase,
  85.                      mreg(__d0) LONG argDPKVersion,
  86.                      mreg(__d1) LONG argDPKRevision)
  87. {
  88.   DPKBase = argDPKBase;
  89.   GVBase  = argGVBase;
  90.   Public  = argModule->Public;
  91.   CNFBase = argModule->ModBase;
  92.  
  93.   if ((argDPKVersion < DPKVersion) OR
  94.      ((argDPKVersion IS DPKVersion) AND (argDPKRevision < DPKRevision))) {
  95.      DPrintF("!Config:","This module requires V%d.%d of the dpkernel.library.",DPKVersion,DPKRevision);
  96.   }
  97.   else {
  98.      if (FileMod = Get(ID_MODULE|GET_NOTRACK)) {
  99.         FileMod->Number = MOD_FILES;
  100.         if (Init(FileMod,NULL)) {
  101.            FILBase = FileMod->ModBase;
  102.  
  103.            if (StrMod = Get(ID_MODULE|GET_NOTRACK)) {
  104.               StrMod->Number = MOD_STRINGS;
  105.               if (Init(StrMod,NULL)) {
  106.                  STRBase = StrMod->ModBase;
  107.  
  108.                  if (ConfigObject = AddSysObjectTags(ID_CONFIG, ID_CONFIG, "Config",
  109.                        TAGS, NULL,
  110.                        SOA_FileExtension, "cnf;config",
  111.                        SOA_FileDesc,      "Config File",
  112.                        SOA_Free,          CON_Free,
  113.                        SOA_Get,           CON_Get,
  114.                        SOA_Init,          CON_Init,
  115.                        SOA_Load,          CON_Load,
  116.                        SOA_FieldArray,    ConfigFields,
  117.                        SOA_FieldTotal,    CONFIG_FIELDS,
  118.                        SOA_FieldSize,     sizeof(struct Field),
  119.                        TAGEND)) {
  120.  
  121.                     return(ERR_OK);
  122.                  }
  123.               }
  124.            }
  125.         }
  126.      }
  127.   }
  128.  
  129.   FreeModule();
  130.   return(ERR_FAILED);
  131. }
  132.  
  133. /************************************************************************************
  134. ** Command:  Open()
  135. **
  136. ** Called when our module is being opened for a second time...
  137. */
  138.  
  139. LIBFUNC LONG CMDOpen(mreg(__a0) struct Module *Module)
  140. {
  141.   if ((Module) AND (Public)) {
  142.      Module->FunctionList = JumpTableV1;
  143.      Public->OpenCount++;
  144.      return(ERR_OK);
  145.   }
  146.   else return(ERR_FAILED);
  147. }
  148.  
  149. /************************************************************************************
  150. ** Command:  Expunge()
  151. ** Synopsis: LONG Expunge(void);
  152. **
  153. ** Called on expunge - if no program has us opened then we can give permission to
  154. ** have us shut us down.
  155. **
  156. */
  157.  
  158. LIBFUNC LONG CMDExpunge(void)
  159. {
  160.   if (Public) {
  161.      if (Public->OpenCount IS NULL) {
  162.         FreeModule();
  163.         return(ERR_OK); /* Okay to expunge */
  164.      }
  165.   }
  166.   else DPrintF("!Config:","I have no Public base reference.");
  167.  
  168.   return(ERR_FAILED); /* Do not expunge */
  169. }
  170.  
  171. /************************************************************************************
  172. ** Command:  Close()
  173. ** Synopsis: void Close(*Module [a0]);
  174. */
  175.  
  176. LIBFUNC void CMDClose(mreg(__a0) struct Module *Module)
  177. {
  178.   if (Public) Public->OpenCount--;
  179. }
  180.  
  181. /************************************************************************************
  182. ** Internal: FreeModule()
  183. ** Short:    Frees any allocations made in the opening of our module.
  184. */
  185.  
  186. void FreeModule(void) {
  187.   if (ConfigObject) { RemSysObject(ConfigObject); ConfigObject = NULL; }
  188.   if (FileMod)      { Free(FileMod); FileMod = NULL; }
  189.   if (StrMod)       { Free(StrMod);  StrMod  = NULL; }
  190. }
  191.  
  192. /***********************************************************************************
  193. ** Function: ReadConfigInt()
  194. ** Synopsis: LONG ReadConfigInt(*Config [a0], BYTE *Section [a1], BYTE *Item [a2])
  195. */
  196.  
  197. LIBFUNC LONG LIBReadConfigInt(mreg(__a0) struct Config *Config,
  198.                               mreg(__a1) BYTE *Section, mreg(__a2) BYTE *Item)
  199. {
  200.    LONG integer;
  201.    BYTE *str;
  202.  
  203.    if ((Config IS NULL) OR (Section IS NULL) OR (Item IS NULL) OR (Config->Head.ID != ID_CONFIG)) {
  204.       DPrintF("ReadInt()","Bad/NULL arguments.");
  205.    }
  206.    else {
  207.       if (str = ReadConfig(Config,Section,Item)) {
  208.          integer = StrToInt(str);
  209.          return(integer);
  210.       }
  211.    }
  212.  
  213.    return(NULL);
  214. }
  215.  
  216. /***********************************************************************************
  217. ** Function: ReadConfig()
  218. ** Synopsis: ReadConfig(*FileName, BYTE *Section, *Item)
  219. ** Short:    Returns a string pointing to the item data.
  220. */
  221.  
  222. LIBFUNC BYTE * LIBReadConfig(mreg(__a0) struct Config *Config, mreg(__a1) BYTE *Section,
  223.                              mreg(__a2) BYTE *Item)
  224. {
  225.    LONG i;
  226.    struct ConEntry *Entry;
  227.  
  228.    if (Item) {
  229.       DPrintF("4ReadConfig()","Config: $%x, \"%s\", \"%s\"",Config,Section,Item);
  230.    }
  231.    else DPrintF("4ReadConfig()","Config: $%x, Array: \"%s\"",Config,Section);
  232.  
  233.    if ((Config IS NULL) OR (Section IS NULL) OR (Config->Head.ID != ID_CONFIG)) {
  234.       DPrintF("!ReadConfig:","Bad arguments.");
  235.       return(NULL);
  236.    }
  237.  
  238.    if (Entry = Config->Entries) {
  239.       for (i=Config->AmtEntries; i > 0; i--) {
  240.          if (StrCompare(Section,Entry->Section,NULL,FALSE) IS TRUE) {
  241.             if (Item) {
  242.                if (StrCompare(Item,Entry->Item,NULL,FALSE) IS TRUE) {
  243.                   return(Entry->Data);
  244.                }
  245.             }
  246.             else return(Entry->Data);
  247.          }
  248.          Entry++;
  249.       }
  250.  
  251.       if (Item)
  252.          DPrintF("3ReadConfig:","Could not find item %s:%s.",Section,Item);
  253.       else
  254.          DPrintF("3ReadConfig:","Could not find array %s.",Section);
  255.    }
  256.    else DPrintF("!ReadConfig:","Corruption to Config->Entries (NULL).");
  257.  
  258.    return(NULL);
  259. }
  260.  
  261. /**********************************************************************************/
  262.  
  263. #include "config_init.c"
  264.  
  265.